package org.example.volunteerend.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.example.volunteerend.entity.AdminInvitationCode;

@Mapper
public interface AdminInvitationCodeMapper {
    
    /**
     * 生成管理员邀请码
     */
    @Insert("INSERT INTO admin_invitation_codes (invitation_code, is_used, created_by, created_time) " +
            "VALUES (#{code.invitationCode}, FALSE, #{code.createdBy}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "code.codeId")
    void insert(@Param("code") AdminInvitationCode code);
    
    /**
     * 查询邀请码是否存在且未使用
     */
    @Select("SELECT * FROM admin_invitation_codes WHERE invitation_code = #{code} AND is_used = FALSE")
    AdminInvitationCode findValidCode(@Param("code") String code);
    
    /**
     * 更新邀请码状态为已使用
     */
    @Update("UPDATE admin_invitation_codes SET is_used = TRUE, used_by = #{userId}, used_time = NOW() " +
            "WHERE invitation_code = #{code} AND is_used = FALSE")
    int updateCodeUsed(@Param("code") String code, @Param("userId") Integer userId);
    
    /**
     * 根据ID查询邀请码
     */
    @Select("SELECT * FROM admin_invitation_codes WHERE code_id = #{codeId}")
    AdminInvitationCode findById(@Param("codeId") Integer codeId);
} 